using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TestingMvvm.Services;
using Contact = TestingMvvm.Models.Contact;

namespace TestingMvvm.ViewModels;

public class MainPageViewModel : ObservableObject {
    private readonly IContactStorage _contactStorage;

    public MainPageViewModel(IContactStorage contactStorage) {
        _contactStorage = contactStorage ??
            throw new ArgumentNullException(nameof(contactStorage));
    }

    private string _status;

    public string Status {
        get => _status;
        set => SetProperty(ref _status, value);
    }

    public ObservableCollection<Contact> Contacts { get; } = new();

    private RelayCommand _loadCommand;

    public RelayCommand LoadCommand =>
        _loadCommand ??= new RelayCommand(async () => {
            await LoadCommandFunction();
        });

    public async Task LoadCommandFunction() {
        Status = "Loading...";

        var contacts = _contactStorage.List();
        await Task.Delay(300);

        foreach (var contact in contacts) {
            Contacts.Add(contact);
        }

        Status = "Loaded";
    }
}